package com.bjesc_b2c.app.appraiset.camera;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import org.json.JSONException;

/**
 * Created by up_girl on 2017/3/30.
 */
public class DisplayUtil {

    private static final String TAG = "DisplayUtil";
    /**
     * 获取分辨率
     *
     * @param context
     * @throws JSONException
     */
    public static int getScreenWidth(Context context) {
        int reWidth = 0;
        try {
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            window.getDefaultDisplay().getMetrics(metrics);
            int width = metrics.widthPixels;
            int height = metrics.heightPixels;
            int temp;
            if (width > height) {
                temp = width;
                width = height;
                height = temp;
            }
            reWidth = width;
        } catch (Exception e2) {
            Log.e(TAG, "read resolution fail");

        }
        return reWidth;
    }

    /**
     * 获取分辨率
     *
     * @param context
     * @throws JSONException
     */
    public static int getScreenHeight(Context context) {
        int reHeight = 0;
        try {
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            window.getDefaultDisplay().getMetrics(metrics);
            int width = metrics.widthPixels;
            int height = metrics.heightPixels;
            int temp;
            if (width > height) {
                temp = width;
                width = height;
                height = temp;
            }
            reHeight = height;
        } catch (Exception e2) {
            Log.e(TAG, "read resolution fail");

        }
        return reHeight;
    }

    /**
     * 获取屏幕宽度和高度，单位为px
     *
     * @param context
     * @return
     */
    public static Point getScreenMetrics(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int w_screen = dm.widthPixels;
        int h_screen = dm.heightPixels;
        return new Point(w_screen, h_screen);

    }

    /**
     * 获取屏幕长宽比
     *
     * @param context
     * @return
     */
    public static float getScreenRate(Context context) {
        Point P = getScreenMetrics(context);
        float H = P.y;
        float W = P.x;
        return (H / W);
    }

}
